<?
	require("required/dbconnect.php");
	require_once("../required/email.php");
	require("required/share_function.php");
	 
	
	
	$get_info=mysql_query("select website from os_contactinfo where id=1")or die(mysql_error());
	$i=mysql_fetch_array($get_info,MYSQL_ASSOC);
	
	
	$email_subject=$_POST['email_subject'];
	$email_content=$_POST['email_content'];
	
	$email_content=str_replace("/userfiles/", $i['website']."userfiles/", $email_content);
	
	if(!empty($_POST['send_to']))
	{
		$send_to=$_POST['send_to'];
		foreach($send_to as $item)
		{	
			
			if($item==1)
			{
				$get=mysql_query("select * from os_email_subscriber where status=1")or die(mysql_error())." line 20";		
				while($a=mysql_fetch_array($get,MYSQL_ASSOC))
				{
					
					$email_arr[]=array("first_name"=>$a['sub_first_name'],"last_name"=>$a['sub_last_name'],"email"=>$a['sub_email']);				
				}
			}
			
			if($item==2)
			{
				$get=mysql_query("select * from os_email_subscriber where status=0")or die(mysql_error())." line 20";		
				while($a=mysql_fetch_array($get,MYSQL_ASSOC))
				{
					$email_arr[]=array("first_name"=>$a['sub_first_name'],"last_name"=>$a['sub_last_name'],"email"=>$a['sub_email']);				
				}
			}
			
			if($item==3)
			{
				$get=mysql_query("select * from os_member where status='activated'")or die(mysql_error())." line 20";		
				while($a=mysql_fetch_array($get,MYSQL_ASSOC))
				{
					$email_arr[]=array("first_name"=>$a['name'],"last_name"=>$a['last_name'],"email"=>$a['email']);				
				}
			}
			
			if($item==4)
			{
				$get=mysql_query("select * from os_member where status<>'activated'")or die(mysql_error())." line 20";		
				while($a=mysql_fetch_array($get,MYSQL_ASSOC))
				{
					$email_arr[]=array("first_name"=>$a['name'],"last_name"=>$a['last_name'],"email"=>$a['email']);				
				}
			}
		}
	}
	
	$insert=mysql_query("insert into os_email_record(email_subject,email_content,created_datetime)values('$email_subject','$email_content',now())")or die(mysql_error())."line 17";;	
	$id=mysql_insert_id();
	
	
	
	$get_info=mysql_query("select * from os_contactinfo where id=1");
	$i=mysql_fetch_array($get_info,MYSQL_ASSOC);
	$websitename_eng=$i['website_name'];
	$from_email=$i['email'];
	
	
	//echo "size of arr ".count($email_arr);
	
	if(!empty($email_arr))
	{
		foreach($email_arr as $email_item)
		{	
			
			$first_name=$email_item["first_name"];
			
			$last_name=$email_item["last_name"];
			$email=$email_item["email"];
			$keyword=array("[%firstname%]","[%lastname%]","[%email%]");
			$replace=array($first_name,$last_name,$email);
			$new_content=str_replace($keyword, $replace, $email_content);
			$subject=str_replace($keyword, $replace, $email_subject);
			
			if($email_item['email']!="")
			{
				$sub_email=trim($email_item['email']);
				
				if(email_send($new_content,$sub_email,"",$subject,$websitename_eng,$from_email))
				{
					mysql_query("update os_email_record set total_sent=total_sent+1 where id=".$id)or die(mysql_error())."line 40";
				}
			}
		}
		
	
	}
	
	header("Location:email_compose3.php?action=1");
?>

